<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Sign in &laquo; Admin</title>
  <link rel="stylesheet" href="/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/assets/css/admin.css">
</head>

<body>
  <div class="login">
    <div class="login-wrap">
      <img class="avatar" src="/assets/img/default.png">
      <!-- 有错误信息时展示 -->
      <div class="errBox alert alert-danger" style="display: none;">
      </div>
      <div class="form-group">
        <label for="email" class="sr-only">邮箱</label>
        <input id="email" type="email" class="form-control" placeholder="邮箱" autofocus>
      </div>
      <div class="form-group">
        <label for="password" class="sr-only">密码</label>
        <input id="password" type="password" class="form-control" placeholder="密码">
      </div>
      <button id="submitBtn" class="btn btn-primary btn-block">登 录</button>
    </div>
  </div>

  <script src="/assets/vendors/jquery/jquery.min.js"></script>
  <script src="/assets/vendors/art-template/template-web.js"></script>
  <script type="text/html" id="errBoxTpl">
      <strong>错误！</strong> {{ msg }}
  </script>
  <script>
    $(() => {
      $('#submitBtn').on('click', () => {
        const email = $('#email').val()
        const password = $('#password').val()
        
        if(email.trim().length === 0) {
          $('.errBox').show()
          $('.errBox').html((template('errBoxTpl', { msg: '邮箱不能为空！'})))
          return
        }else if(password.trim().length === 0) {
          $('.errBox').show()
          $('.errBox').html((template('errBoxTpl', { msg: '密码不能为空！'})))
          return
        }

        $.ajax({
          type: 'POST',
          url: '/login',
          data: {
            email: email,
            password: password
          },
          success(res) {
            location.href = 'index.html'
          },
          error(res) {
            const errMsg = JSON.parse(res.responseText)
            $('.errBox').show()
            $('.errBox').html((template('errBoxTpl', { msg: errMsg.message})))
          }
        })

      })

      $('#password').keyup(({ keyCode }) => {
        if(keyCode === 13){
          $('#submitBtn').click()
        }
      })
    })
  </script>
</body>

</html>